package cn.tedu.egweibo4.controller;

import cn.tedu.egweibo4.common.respose.JsonResult;
import cn.tedu.egweibo4.common.respose.StatusCode;
import cn.tedu.egweibo4.mapper.WeiboMapper;
import cn.tedu.egweibo4.pojo.dto.WeiboDTO;
import cn.tedu.egweibo4.pojo.entity.Weibo;
import cn.tedu.egweibo4.pojo.vo.UserVO;
import com.fasterxml.jackson.databind.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发微博功能")
    @PostMapping("insert")
    public JsonResult insertWeibo(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        UserVO userVO =(UserVO) session.getAttribute("user");
        if (userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok();


    }

}
